package xyz.bali16.module.ban.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.application.entity.Role;
import xyz.bali16.module.ban.entity.Ban;
import xyz.bali16.module.ban.model.BanAddParam;
import xyz.bali16.module.ban.model.BanPageParam;
import xyz.bali16.module.ban.model.BanUpdateParam;
import xyz.bali16.module.ban.service.BanService;

import javax.validation.Valid;

import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.application.core.model.Result.failure;
import static xyz.bali16.module.ban.utils.ResultUtils.errorLog;

/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"ban")
@Api(tags = "举报接口")
@AllArgsConstructor

public class BanController extends BaseController{
    public static final Logger logger = LoggerFactory.getLogger(BanController.class);
    @Autowired
    BanService banService;

    @GetMapping
    @ApiOperation("分页条件查询举报列表")
    //@PreAuthorize("@auth.permission('plugin:ban:query')")
    public Result<IPage<Ban>> pageQuery(@Valid BanPageParam param) {
        IPage<Ban> banIPage = banService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, banIPage);
    }

    @PostMapping
    //@PreAuthorize("@auth.permission('plugin:ban:add')")
    @ApiOperation("添加举报")
    public Result<String> add(@RequestBody @Valid BanAddParam param) {
        try {
            banService.addBan(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "新增举报 '{}' 信息失败 {}", param.getName(), e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增举报失败", e);
        }
    }

    @PutMapping
    //@PreAuthorize("@auth.permission('plugin:ban:update')")
    @ApiOperation("修改举报")
    public Result<String> update(@RequestBody @Valid BanUpdateParam param) {
        try {
            banService.updateBan(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "更新举报 '{}' 信息失败 {}", param.getId(), e.getMessage());
            return failure(ApiEnum.UPDATE_ERROR, "更新举报失败", e);
        }
    }

    @PutMapping("{banId}/{status}")
    //@PreAuthorize("@auth.permission('plugin:ban:update')")
    @ApiOperation("修改举报状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "banId", value = "举报id", paramType = "path", required = true),
            @ApiImplicitParam(name = "status", value = "状态（1启用, 0停用）", paramType = "path", required = true)
    })
    public Result<Role> updateStatus(@PathVariable("banId") String banId, @PathVariable("status") Integer status) {
        String message = status == 1 ? "启用" : "禁用";
        try {
            banService.updateStatus(banId, status);
            return response(ApiEnum.UPDATE_SUCCESS, message + "成功");
        } catch (Exception e) {
            errorLog(logger, e, "修改举报状态失败. {}", e.getMessage());
            return failure(ApiEnum.UPDATE_ERROR, message + "失败: ", e);
        }
    }


    @DeleteMapping("/{banId}")
    //@PreAuthorize("@auth.permission('plugin:ban:delete')")
    @ApiOperation("删除举报")
    @ApiImplicitParam(name = "banId", value = "举报id", paramType = "path", required = true)
    public Result<String> deleteBan(@PathVariable("banId") String banId) {
        try {
            banService.delete(banId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "删除举报 '{}' 失败. {}", banId, e.getMessage());
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }
}
